package com.zdsoft.finance.meet.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zdsoft.finance.base.service.impl.BaseServiceImpl;
import com.zdsoft.finance.common.base.CustomRepository;
import com.zdsoft.finance.common.exception.BusinessException;
import com.zdsoft.finance.meet.entity.Meeting;
import com.zdsoft.finance.meet.entity.MeetingMember;
import com.zdsoft.finance.meet.entity.MeetingProject;
import com.zdsoft.finance.meet.repository.MeetingMemberRepository;
import com.zdsoft.finance.meet.repository.MeetingProjectRepository;
import com.zdsoft.finance.meet.repository.MeetingRepository;
import com.zdsoft.finance.meet.service.MeetingMemberService;
import com.zdsoft.finance.spi.meeting.dto.MeetingProjectStatus;
import com.zdsoft.finance.spi.meeting.dto.MeetingStatus;
import com.zdsoft.framework.core.common.util.DateHelper;
import com.zdsoft.framework.core.common.util.ObjectHelper;

@Service("meetingMemberService")
public class MeetingMemberServiceImpl extends BaseServiceImpl<MeetingMember, CustomRepository<MeetingMember, String>>
		implements MeetingMemberService {

	@Autowired
	MeetingMemberRepository meetingMemberRepository;

	@Autowired
	MeetingProjectRepository meetingProjectRepository;

	@Autowired
	MeetingRepository meetingRepository;

	@Override
	@Transactional
	public void saveMeetingMembers(List<MeetingMember> meetingMembers, List<String> meetingProjectIds)
			throws Exception {

		if (ObjectHelper.isNotEmpty(meetingProjectIds)) {

			List<MeetingMember> deleteMembers = meetingMemberRepository.findByMeetingProjectIds(meetingProjectIds);

			// 判断是否有委员，如果有，则删除原有委员
			if (ObjectHelper.isNotEmpty(deleteMembers) && deleteMembers.size() != 0) {
				for (MeetingMember meetingMember : deleteMembers) {
					this.logicDelete(meetingMember);
				}
			}

		}

		// 保存现有委员
		for (MeetingMember meetingMember : meetingMembers) {
			this.saveEntity(meetingMember);
		}
	}

	@Override
	@Transactional
	public MeetingMember singleVote(MeetingMember meetingMember) throws BusinessException {

		if (ObjectHelper.isEmpty(meetingMember) || ObjectHelper.isEmpty(meetingMember.getId())) {
			throw new BusinessException("10010003", "传入参数有错误");
		}

		MeetingMember oldMeetingMember = meetingMemberRepository.findOne(meetingMember.getId());
		oldMeetingMember.setScore(meetingMember.getScore());
		oldMeetingMember.setVote(meetingMember.getVote());
		oldMeetingMember.setOpinion(meetingMember.getOpinion());
		oldMeetingMember.setVotingDateTime(DateHelper.dateToLong(new Date(), "yyyyMMddHHmmss"));
		oldMeetingMember.setIsSubmit(true);

		oldMeetingMember = meetingMemberRepository.updateEntity(oldMeetingMember);

		checkMeeting(oldMeetingMember);
		return oldMeetingMember;
	}

	@Override
	@Transactional
	public void singleVotes(MeetingMember meetingMember) throws BusinessException {
		String[] meetingMemberIds = meetingMember.getMeetingMemberIds().split(",");
		if (ObjectHelper.isNotEmpty(meetingMemberIds) && meetingMemberIds.length != 0) {

			for (String id : meetingMemberIds) {
				MeetingMember oldMeetingMember = meetingMemberRepository.findOne(id);
				oldMeetingMember.setScore(meetingMember.getScore());
				oldMeetingMember.setVote(meetingMember.getVote());
				oldMeetingMember.setOpinion(meetingMember.getOpinion());
				oldMeetingMember.setVotingDateTime(DateHelper.dateToLong(new Date(), "yyyyMMddHHmmss"));
				oldMeetingMember.setIsSubmit(true);
				oldMeetingMember = meetingMemberRepository.updateEntity(oldMeetingMember);

				checkMeeting(oldMeetingMember);
			}

		} else {
			throw new BusinessException("10010003", "传入参数有错误");
		}

	}

	public void checkMeeting(MeetingMember meetingMember) {
		MeetingProject meetingProject = meetingMember.getMeetingProject();

		// 判断是否为第一次投票
		if (MeetingProjectStatus.MEET_PROJECT_STATE_TOVOTE.value == meetingProject.getState()) {
			meetingProject.setState(MeetingProjectStatus.MEET_PROJECT_STATE_VOTEING.value);
			meetingProject = meetingProjectRepository.updateEntity(meetingProject);
		}
		Meeting meeting = meetingProject.getMeeting();
		if (MeetingStatus.MEET_STATE_PEND.value.equals(meeting.getState())) {
			meeting.setState(MeetingStatus.MEET_STATE_DOING.value);
			meeting.setIsVote(1);
			meeting = meetingRepository.updateEntity(meeting);
		}

		// 判断是否为最后一个投票
		List<String> ids = new ArrayList<String>();
		ids.add(meetingProject.getId());
		List<MeetingMember> meetingMembers = meetingMemberRepository.findByMeetingProjectIds(ids);
		Boolean isOver = true;
		for (MeetingMember voteMember : meetingMembers) {
			if (ObjectHelper.isEmpty(voteMember.getIsSubmit()) || !voteMember.getIsSubmit()) {
				isOver = false;
				break;
			}
		}

		if (isOver) {
			meetingProject.setState(MeetingProjectStatus.MEET_PROJECT_STATE_TOACCR.value);
			meetingProject = meetingProjectRepository.updateEntity(meetingProject);
		}

		List<MeetingProject> meetingProjects = meetingProjectRepository.findByMeetingId(meeting.getId());
		Boolean isEnd = true;
		for (MeetingProject voteMeetingProject : meetingProjects) {
			if (ObjectHelper.isEmpty(voteMeetingProject.getState())
					|| MeetingProjectStatus.MEET_PROJECT_STATE_TOACCR.value != (voteMeetingProject.getState())) {
				isEnd = false;
				break;
			}
		}
		if (isEnd) {
			meeting.setState(MeetingStatus.MEET_STATE_FINAL_END.value);
			meeting = meetingRepository.updateEntity(meeting);
		}
	}

}
